package org.nxt.ask;

import java.awt.TrayIcon;
import java.io.IOException;
import java.util.Properties;
import java.util.StringTokenizer;

import org.asteriskjava.manager.AuthenticationFailedException;
import org.asteriskjava.manager.ManagerConnection;
import org.asteriskjava.manager.ManagerConnectionFactory;
import org.asteriskjava.manager.TimeoutException;
import org.asteriskjava.manager.action.QueueAddAction;
import org.asteriskjava.manager.action.QueueLogAction;
import org.asteriskjava.manager.action.QueueRemoveAction;
import org.asteriskjava.manager.response.ManagerResponse;

public class Asterisk {
	private ManagerConnection managerConnection;
	

	public Asterisk() throws IOException {
		
	}

	public String login(Properties props, TrayIcon trayIcon) throws IOException, AuthenticationFailedException, TimeoutException {
		String resp = "";		

		String iface = "Local/"+props.getProperty("extension")+"@"+props.getProperty("context");
		String queues = props.getProperty("queues");
		StringTokenizer st = new StringTokenizer(queues, ",");
		
		ManagerResponse originateResponse;
		QueueAddAction queueAddAction;// = new QueueAddAction("ventas", iface);
		QueueLogAction queueLogAction;

		String queue = "";
		while(st.hasMoreTokens()) {
//			queue = st.nextToken();
//			queueLogAction = new QueueLogAction(queue, )
			queueAddAction = new QueueAddAction(queue, iface);
			originateResponse = managerConnection.sendAction(queueAddAction, 30000);
			resp = queue + " login: " + originateResponse.getResponse() + "\n";
		}
		
		queueAddAction = null;
		originateResponse = null;
		
		System.out.println(resp);

		return resp;
	}

	public String logoff(Properties props) throws IOException, AuthenticationFailedException, TimeoutException {
		if(managerConnection == null) return "Error";
		
		String resp = "";
		
		String iface = "Local/"+props.getProperty("extension")+"@"+props.getProperty("context");
		String queues = props.getProperty("queues");
		StringTokenizer st = new StringTokenizer(queues, ",");
		
		ManagerResponse originateResponse;
		QueueRemoveAction queueRemoveAction;// = new QueueAddAction("ventas", iface);

		String queue = "";
		while(st.hasMoreTokens()) {
			queue = st.nextToken();
			queueRemoveAction = new QueueRemoveAction(queue, iface);
			originateResponse = managerConnection.sendAction(queueRemoveAction, 30000);
			resp = queue + " logout: " + originateResponse.getResponse() + "\n";
		}
		queueRemoveAction = null;
		originateResponse = null;

		
		
		return resp;
	}
	
	public boolean connect(Properties props, TrayIcon trayIcon) throws IOException, AuthenticationFailedException, TimeoutException {
		boolean resp = false;
		
		ManagerConnectionFactory factory = new ManagerConnectionFactory(props.getProperty("host"), props.getProperty("user"), props.getProperty("password"));
		managerConnection = factory.createManagerConnection();

		managerConnection.addEventListener(new Eventos(trayIcon));
		managerConnection.login();
		
		return resp;
	}
	
	public boolean disconnect() {
		boolean resp = false;
		
		managerConnection.logoff();
			
		return resp;
	}

}
